/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.mobbuzz.renderer;

import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;
import edu.mobbuzz.bean.Category;
import edu.mobbuzz.bean.RSSChannel;
import edu.mobbuzz.facade.CategoryFacade;
import edu.mobbuzz.facade.LabelFacade;
import edu.mobbuzz.util.theme.Theme;
import java.io.IOException;
import java.util.Vector;

public class RSSChannelRenderer extends Container implements ListCellRenderer{
    private Label rssSourceLbl = new Label("");
    private Label rssTitleLbl = new Label("");
    private Label rssLabellbl = new Label("");
    private Label pic = new Label("");
    private Image icon = null;
    private Label focus = new Label("");
    private LabelFacade labelFacade;
    private CategoryFacade categoryFacade;
    
    
    public RSSChannelRenderer(){
        setLayout(new BorderLayout());
        addComponent(BorderLayout.WEST, pic);
        Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        rssSourceLbl.getStyle().setBgTransparency(0);  
       
        
        rssTitleLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        rssTitleLbl.getStyle().setBgTransparency(0);
        rssTitleLbl.getStyle().setMargin(0, 0, 0, 0);
        
        rssSourceLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL));
        rssSourceLbl.getStyle().setBgTransparency(0);
        rssSourceLbl.getStyle().setMargin(0, 0, 0, 0);
        
        rssLabellbl.getStyle().setBgTransparency(0);
        rssLabellbl.getStyle().setFont(Theme.getFontDescriptionStyle("SansSerif"));
        rssLabellbl.getStyle().setFgColor(0xF59FBC);
        rssLabellbl.getStyle().setMargin(0, 0, 0, 0);
        
        pic.getStyle().setBgTransparency(0);

        cnt.addComponent(rssTitleLbl);
        cnt.addComponent(rssSourceLbl);
        cnt.addComponent(rssLabellbl);

        addComponent(BorderLayout.CENTER, cnt);
        focus.getStyle().setBgTransparency(100);
        focus.getStyle().setBgColor(0xCCCCCC);
    }
    
    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
        RSSChannel rssChannel = (RSSChannel) value;
        StringBuffer labelStr = new StringBuffer();

        try {
            icon = Image.createImage("/icons/rssicon.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        
        Vector labelArr = getLabelFacade().getLabelByRss(rssChannel.getRecId());
        for(int i=0;i<labelArr.size();i++){
            edu.mobbuzz.bean.Label lbl = (edu.mobbuzz.bean.Label)labelArr.elementAt(i);
            Category category = getCategoryFacade().getCategoryById(lbl.getRecIdCategory());
            if(i>0)
                labelStr.append(" - ");
            
            labelStr.append(category.getLabel());
            
        }
        
        rssTitleLbl.setText(rssChannel.getTitle());
        rssSourceLbl.setText(rssChannel.getSource());        
        rssLabellbl.setText(labelStr.toString());
        pic.setIcon(icon);
        
        return this;
    }

    public Component getListFocusComponent(List list) {
        return focus;
    }

    public LabelFacade getLabelFacade() {
        if(labelFacade==null)
            labelFacade = new LabelFacade();
        return labelFacade;
    }

    public CategoryFacade getCategoryFacade() {
        if(categoryFacade==null)
            categoryFacade = new CategoryFacade();
        return categoryFacade;
    }     

}
